package net.wangds.auth.department.comp;

import net.wangds.auth.department.entity.GeneralDepartmentEntity;
import net.wangds.auth.department.entity.GeneralDepartmentJobRef;
import net.wangds.auth.department.entity.GeneralDepartmentRoleRef;
import net.wangds.auth.department.entity.GeneralDepartmentUserRef;
import net.wangds.auth.department.repo.rdbms.DepartmentJobRefRepo;
import net.wangds.auth.department.repo.rdbms.DepartmentJpaRepo;
import net.wangds.auth.department.repo.rdbms.DepartmentRoleRefRepo;
import net.wangds.auth.department.repo.rdbms.DepartmentUserRefRepo;
import net.wangds.auth.job.entity.GeneralJobEntity;
import net.wangds.auth.module.Department;
import net.wangds.auth.module.Job;
import net.wangds.auth.module.Role;
import net.wangds.auth.module.User;
import net.wangds.data.RemovedEnum;
import net.wangds.jdbc.comp.jdbc.JdbcComp;
import net.wangds.jpa.comp.AbstractRemovedComp;
import net.wangds.jpa.repository.RemovableRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

/**
 * 部门组件.
 */
@Component
public class GeneralDepartmentComp extends AbstractRemovedComp<GeneralDepartmentEntity> {


    @Autowired
    DepartmentJpaRepo mmRepoDept;

    @Autowired
    DepartmentUserRefRepo mmJpaDeptUser;

    @Autowired
    DepartmentJobRefRepo mmJpaDeptJob;

    @Autowired
    DepartmentRoleRefRepo mmJpaDeptRole;
    @Autowired
    JdbcComp mmJdbc;

    /**
     * 构造函数.
     * @param repo 数据库操作对象.
     */
    public GeneralDepartmentComp(RemovableRepo<GeneralDepartmentEntity> repo) {
        super(repo);
    }

    @Override
    public Page<GeneralDepartmentEntity> findByKeywords(String keywords, Pageable paging) {
        String sql = "SELECT t1.* FROM auth_department t1\n" +
                "where ( t1.department_name regexp ? or t1.department_desc regexp ? ) and t1.removed = 0";
        return mmJdbc.executeQuery(sql, new Object[]{keywords, keywords}, paging, GeneralDepartmentEntity.class);
    }


    /**
     * 绑定部门用户.
     * @param dept 部门.
     * @param user 用户。
     */
    public void bindUser(Department dept, User user){
        String deptId = dept.getId();
        String uid = user.getId();

        GeneralDepartmentUserRef ref = mmJpaDeptUser.findByDeptIdAndUserId(deptId, uid)
                .orElseGet(() -> GeneralDepartmentUserRef.builder().deptId(deptId).userId(uid).build());
        //mmJpaDeptUser.save

        ref.setRemoved(RemovedEnum.NORMAL);

        mmJpaDeptUser.saveAndFlush(ref);
    }

    /**
     * 解除部门和员工的绑定.
     * @param dept 部门.
     * @param user 用户.
     */
    public void unbindUser(Department dept, User user){
        String deptId = dept.getId();
        String uid = user.getId();
        mmJpaDeptUser.findByDeptIdAndUserId(deptId, uid).ifPresent(r->{
            mmJpaDeptUser.delete(r);
            mmJpaDeptUser.flush();
        });
    }

    public void bindJob(Department dept, Job job) {

        String deptId = dept.getId();
        String jobId = job.getId();

        GeneralDepartmentJobRef ref = mmJpaDeptJob.findByDeptIdAndJobId(deptId, jobId)
                .orElseGet(() -> GeneralDepartmentJobRef.builder().deptId(deptId).jobId(jobId).build());
        ref.setRemoved(RemovedEnum.NORMAL);

        mmJpaDeptJob.saveAndFlush(ref);

    }
    public void unbindJob(Department dept, Job job) {
        String deptId = dept.getId();
        String jobId = job.getId();
        mmJpaDeptJob.findByDeptIdAndJobId(deptId, jobId).ifPresent(ref->{
            mmJpaDeptJob.delete(ref);
            mmJpaDeptJob.flush();
        });
    }
    public void bindRole(Department dept, Role role) {
        String deptId = dept.getId();
        String roleId = role.getId();

        GeneralDepartmentRoleRef ref = mmJpaDeptRole.findByDeptIdAndRoleId(deptId, roleId)
                .orElseGet(() -> GeneralDepartmentRoleRef.builder().deptId(deptId).roleId(roleId).build());
        ref.setRemoved(RemovedEnum.NORMAL);

        mmJpaDeptRole.saveAndFlush(ref);

    }
    public void unbindRole(Department dept, Role role) {
        String deptId = dept.getId();
        String roleId = role.getId();
        mmJpaDeptRole.findByDeptIdAndRoleId(deptId, roleId).ifPresent(ref->{
            mmJpaDeptRole.delete(ref);
            mmJpaDeptRole.flush();
        });
    }

    public List<GeneralDepartmentJobRef> findDeptJobRef(Department dept) {
        return mmJpaDeptJob.findByDeptIdAndRemoved(dept.getId(), RemovedEnum.NORMAL);
    }


    public List<GeneralDepartmentRoleRef> findDeptRoleRef(Department dept) {
        return mmJpaDeptRole.findByDeptIdAndRemoved(dept.getId(), RemovedEnum.NORMAL);
    }
}
